package rz.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
 * xml中配置,两种方式
 *  <bean id="sqlTime" class="test.com.wos.manage.intercept.SQLExecuteTime"></bean>
 *  1.<aop:aspectj-autoproxy />
    2.<!--配置aop --> 
    <aop:config> 
        <aop:aspect id="aop22" ref="sqlTime">
            <!--定义切入点  --> 
            <aop:pointcut id="mycut" 
                expression="execution(* com.wos.manage.database.dao.impl.*.*(..))" /> 
            <!-- pointcut-ref :切入点引用--> 
            <!--<aop:before pointcut-ref="mycut" method="doAccessCheck" arg-names="name" />--> 
            <!-- <aop:after-returning pointcut-ref="mycut" 
                method="doAfterReturning" returning="result" /> 
            <aop:after-throwing pointcut-ref="mycut" 
                method="doAfterThrowing" throwing="e" /> 
            <aop:after pointcut-ref="mycut" method="doAfter" />  -->
            <aop:around pointcut-ref="mycut" method="doBasicProfiling" /> 
        </aop:aspect> 
    </aop:config>
 * @author dhding
 *
 */

@Aspect
public class SQLExecuteTime {
	@Pointcut("execution (* com.wos.manage.database.dao.impl.*.*(..))")
	private void anyMethod() {
	}// 声明一个切入点

	@Before("anyMethod() && args(name)")
	// 定义前置通知,拦截的方法不但要满足声明的切入点的条件,而且要有一个String类型的输入参数,否则不会拦截
	public void doAccessCheck(String name) {
		//System.out.println("前置通知:" + name);
	}

	@AfterReturning(pointcut = "anyMethod()", returning = "result")
	// 定义后置通知,拦截的方法的返回值必须是int类型的才能拦截
	public void doAfterReturning(int result) {
		//System.out.println("后置通知:" + result);
	}

	@AfterThrowing(pointcut = "anyMethod()", throwing = "e")
	// 定义例外通知
	public void doAfterThrowing(Exception e) {
		//System.out.println("例外通知:" + e);
	}

	@After("anyMethod()")
	// 定义最终通知
	public void doAfter() {
		//System.out.println("最终通知");
	}

	@Around("anyMethod()")
	// 定义环绕通知
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
		// if(){//判断用户是否在权限
		//System.out.println("进入方法");
		long stime = System.currentTimeMillis();
		Object result = pjp.proceed();
		//System.out.println("退出方法");
		System.out.println(pjp.getSignature() + "执行时间: " + (System.currentTimeMillis() - stime) + " ms");
		// }
		return result;
	}
}
